package labox.innovation.gameserver.network.clientpackets;

import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.network.L2GameClient;
import labox.innovation.gameserver.network.serverpackets.ExRpItemLink;
import labox.innovation.gameserver.templates.item.L2Item;

/**
 * 
 * @author KenM
 */
public class RequestExRqItemLink extends L2GameClientPacket {
	private long _objectId = -1;

	private int _flag; // 0:objId 1:模板id

	private int _itemId = -1;

	/**
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#getType()
	 */
	@Override
	public String getType() {
		return "[C] 0x71 RequestExRqItemLink";
	}

	/**
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#readImpl()
	 */
	@Override
	protected void readImpl() {
		_flag = readByte();
		if (_flag == 0)
			_objectId = readLong();
		else
			_itemId = readInt();
	}

	/**
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#runImpl()
	 */
	@Override
	protected void runImpl() {
		L2GameClient client = this.getClient();
		if (_objectId != -1) {
			if (client != null) {
				L2Object object = FuzeWorld.getInstance().findObject(_objectId);
				if (object != null && object instanceof L2ItemInstance) {
					L2ItemInstance item = (L2ItemInstance) object;
					L2ItemInstance iteme = client.getActiveChar().getInventory().getPaperdollItemByL2ItemId(item.getItem().getBodyPart());
					client.sendPacket(new ExRpItemLink(item, iteme));
				}
			}
		} else {
			L2Item item = ItemTable.getInstance().getTemplate(_itemId);
			if (item != null) {
				L2ItemInstance iteme = client.getActiveChar().getInventory().getPaperdollItemByL2ItemId(item.getBodyPart());
				client.sendPacket(new ExRpItemLink(item, iteme));
			}
		}

	}

}
